package o;

import android.os.ConditionVariable;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
import com.google.android.exoplayer2.upstream.cache.Cache.c;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public final class amn implements Cache {
    private static boolean ˏ;
    private static final HashSet<File> ॱ = new HashSet();
    private long ʼ;
    private final File ˊ;
    private final ama ˋ;
    private final amk ˎ;
    private final HashMap<String, ArrayList<c>> ॱॱ;
    private boolean ᐝ;

    public /* synthetic */ amd ˋ(String str, long j) throws InterruptedException, CacheException {
        return ॱ(str, j);
    }

    @Nullable
    public /* synthetic */ amd ˏ(String str, long j) throws CacheException {
        return ˎ(str, j);
    }

    public amn(File file, ama o_ama) {
        this(file, o_ama, null, false);
    }

    public amn(File file, ama o_ama, byte[] bArr, boolean z) {
        this(file, o_ama, new amk(file, bArr, z));
    }

    amn(File file, ama o_ama, amk o_amk) {
        if (ॱ(file)) {
            this.ˊ = file;
            this.ˋ = o_ama;
            this.ˎ = o_amk;
            this.ॱॱ = new HashMap();
            final ConditionVariable conditionVariable = new ConditionVariable();
            new Thread(this, "SimpleCache.initialize()") {
                final /* synthetic */ amn ॱ;

                public void run() {
                    synchronized (this.ॱ) {
                        conditionVariable.open();
                        this.ॱ.ˋ();
                        this.ॱ.ˋ.ˋ();
                    }
                }
            }.start();
            conditionVariable.block();
            return;
        }
        throw new IllegalStateException("Another SimpleCache instance uses the folder: " + file);
    }

    public synchronized long ˏ() {
        amm.ˏ(!this.ᐝ);
        return this.ʼ;
    }

    public synchronized aml ॱ(String str, long j) throws InterruptedException, CacheException {
        aml ˎ;
        while (true) {
            ˎ = ˎ(str, j);
            if (ˎ == null) {
                wait();
            }
        }
        return ˎ;
    }

    @Nullable
    public synchronized aml ˎ(String str, long j) throws CacheException {
        aml ॱ;
        boolean z = true;
        synchronized (this) {
            if (this.ᐝ) {
                z = false;
            }
            amm.ˏ(z);
            aml ʽ = ʽ(str, j);
            if (ʽ.ˏ) {
                try {
                    ॱ = this.ˎ.ˏ(str).ॱ(ʽ);
                    ॱ(ʽ, (amd) ॱ);
                } catch (CacheException e) {
                    ॱ = ʽ;
                }
            } else {
                amc ˊ = this.ˎ.ˊ(str);
                if (ˊ.ˎ()) {
                    ॱ = null;
                } else {
                    ˊ.ˏ(true);
                    ॱ = ʽ;
                }
            }
        }
        return ॱ;
    }

    public synchronized File ॱ(String str, long j, long j2) throws CacheException {
        amc ˏ;
        amm.ˏ(!this.ᐝ);
        ˏ = this.ˎ.ˏ(str);
        amm.ॱ(ˏ);
        amm.ˏ(ˏ.ˎ());
        if (!this.ˊ.exists()) {
            this.ˊ.mkdirs();
            ॱ();
        }
        this.ˋ.ˎ(this, str, j, j2);
        return aml.ॱ(this.ˊ, ˏ.ॱ, j, System.currentTimeMillis());
    }

    public synchronized void ˊ(File file) throws CacheException {
        boolean z = true;
        synchronized (this) {
            boolean z2;
            amm.ˏ(!this.ᐝ);
            aml ˎ = aml.ˎ(file, this.ˎ);
            if (ˎ != null) {
                z2 = true;
            } else {
                z2 = false;
            }
            amm.ˏ(z2);
            amc ˏ = this.ˎ.ˏ(ˎ.ˎ);
            amm.ॱ(ˏ);
            amm.ˏ(ˏ.ˎ());
            if (file.exists()) {
                if (file.length() == 0) {
                    file.delete();
                } else {
                    long ˊ = amg.ˊ(ˏ.ˏ());
                    if (ˊ != -1) {
                        if (ˎ.ॱ + ˎ.ˋ > ˊ) {
                            z = false;
                        }
                        amm.ˏ(z);
                    }
                    ˋ(ˎ);
                    this.ˎ.ˊ();
                    notifyAll();
                }
            }
        }
    }

    public synchronized void ॱ(amd o_amd) {
        boolean z = false;
        synchronized (this) {
            if (!this.ᐝ) {
                z = true;
            }
            amm.ˏ(z);
            amc ˏ = this.ˎ.ˏ(o_amd.ˎ);
            amm.ॱ(ˏ);
            amm.ˏ(ˏ.ˎ());
            ˏ.ˏ(false);
            this.ˎ.ˎ(ˏ.ˋ);
            notifyAll();
        }
    }

    public synchronized void ˎ(amd o_amd) {
        amm.ˏ(!this.ᐝ);
        ˏ(o_amd);
    }

    public synchronized long ˊ(String str, long j, long j2) {
        amc ˏ;
        amm.ˏ(!this.ᐝ);
        ˏ = this.ˎ.ˏ(str);
        return ˏ != null ? ˏ.ˏ(j, j2) : -j2;
    }

    public synchronized void ˊ(String str, long j) throws CacheException {
        ami o_ami = new ami();
        amg.ˋ(o_ami, j);
        ॱ(str, o_ami);
    }

    public synchronized long ˎ(String str) {
        return amg.ˊ(ˊ(str));
    }

    public synchronized void ॱ(String str, ami o_ami) throws CacheException {
        amm.ˏ(!this.ᐝ);
        this.ˎ.ॱ(str, o_ami);
        this.ˎ.ˊ();
    }

    public synchronized amh ˊ(String str) {
        amm.ˏ(!this.ᐝ);
        return this.ˎ.ॱ(str);
    }

    private aml ʽ(String str, long j) throws CacheException {
        amc ˏ = this.ˎ.ˏ(str);
        if (ˏ == null) {
            return aml.ˋ(str, j);
        }
        while (true) {
            aml ˋ = ˏ.ˋ(j);
            if (!ˋ.ˏ || ˋ.ˊ.exists()) {
                return ˋ;
            }
            ॱ();
        }
    }

    private void ˋ() {
        if (this.ˊ.exists()) {
            this.ˎ.ॱ();
            File[] listFiles = this.ˊ.listFiles();
            if (listFiles != null) {
                for (File file : listFiles) {
                    if (!file.getName().equals("cached_content_index.exi")) {
                        aml ˎ = file.length() > 0 ? aml.ˎ(file, this.ˎ) : null;
                        if (ˎ != null) {
                            ˋ(ˎ);
                        } else {
                            file.delete();
                        }
                    }
                }
                this.ˎ.ˋ();
                try {
                    this.ˎ.ˊ();
                    return;
                } catch (Throwable e) {
                    amz.ˊ("SimpleCache", "Storing index file failed", e);
                    return;
                }
            }
            return;
        }
        this.ˊ.mkdirs();
    }

    private void ˋ(aml o_aml) {
        this.ˎ.ˊ(o_aml.ˎ).ˋ(o_aml);
        this.ʼ += o_aml.ˋ;
        ॱ(o_aml);
    }

    private void ˏ(amd o_amd) {
        amc ˏ = this.ˎ.ˏ(o_amd.ˎ);
        if (ˏ != null && ˏ.ˏ(o_amd)) {
            this.ʼ -= o_amd.ˋ;
            this.ˎ.ˎ(ˏ.ˋ);
            ˊ(o_amd);
        }
    }

    private void ॱ() {
        ArrayList arrayList = new ArrayList();
        for (amc ˋ : this.ˎ.ˏ()) {
            Iterator it = ˋ.ˋ().iterator();
            while (it.hasNext()) {
                amd o_amd = (amd) it.next();
                if (!o_amd.ˊ.exists()) {
                    arrayList.add(o_amd);
                }
            }
        }
        for (int i = 0; i < arrayList.size(); i++) {
            ˏ((amd) arrayList.get(i));
        }
    }

    private void ˊ(amd o_amd) {
        ArrayList arrayList = (ArrayList) this.ॱॱ.get(o_amd.ˎ);
        if (arrayList != null) {
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                ((c) arrayList.get(size)).ˊ(this, o_amd);
            }
        }
        this.ˋ.ˊ(this, o_amd);
    }

    private void ॱ(aml o_aml) {
        ArrayList arrayList = (ArrayList) this.ॱॱ.get(o_aml.ˎ);
        if (arrayList != null) {
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                ((c) arrayList.get(size)).ˋ(this, o_aml);
            }
        }
        this.ˋ.ˋ(this, o_aml);
    }

    private void ॱ(aml o_aml, amd o_amd) {
        ArrayList arrayList = (ArrayList) this.ॱॱ.get(o_aml.ˎ);
        if (arrayList != null) {
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                ((c) arrayList.get(size)).ˊ(this, o_aml, o_amd);
            }
        }
        this.ˋ.ˊ(this, o_aml, o_amd);
    }

    private static synchronized boolean ॱ(File file) {
        boolean z;
        synchronized (amn.class) {
            if (ˏ) {
                z = true;
            } else {
                z = ॱ.add(file.getAbsoluteFile());
            }
        }
        return z;
    }
}
